package com.stream.utils;

import com.stream.utils.xss.SQLFilter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 查询参数
 * Created by yebaokang on 2017/5/19.
 */
public class Query extends LinkedHashMap<String,Object>{
    public static final long serialVersionUID = 1L;

    /**
     * 当前页码
     */
    private int page;
    
    /**
     * 每页记录数
     */
    private int limit;

    public Query(Map<String, Object> param) {
        this.putAll(param);

        //每页页数
//        this.page = (int) ConvertUtils.convert(param.get("page"), Integer.TYPE);
        this.page = Integer.parseInt(Objects.toString(param.get("page"), "1"));
        this.limit = Integer.parseInt(Objects.toString(param.get("limit"), "1"));
        this.put("offset", (page -1)*limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = param.get("sidx").toString();
        String order = param.get("order").toString();
        this.put("sidx", SQLFilter.sqlInject(sidx));
        this.put("order", SQLFilter.sqlInject(order));
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
